package com.project.controller.web;

import com.project.entity.UserRole;
import com.project.myBeans.R;
import com.project.myBeans.ResponseBean;
import com.project.service.UserRoleService;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/27 17:56
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/userRole")
@Api(description = "用户角色管理接口",value = "userRole")
public class UserRoleController extends BaseService {
    @Autowired
    private UserRoleService userRoleService;
    /**
     * TateBrown 2018年5月27日
     *
     * @param userRole
     * @return
     */
    @ApiOperation("资源与用户列表相关联")
    @PostMapping("/addBatchByRoleId")
    public R addBatch(@RequestBody UserRole userRole) {
        try {
            userRoleService.addBatch(userRole);
            return R.ok("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("操作失败");

        }

    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param roleid
     * @return
     */
    @ApiOperation("根据roleid查某角色所授权的用户")
    @GetMapping("/findUsers/{roleid}")
    public R findUsers(@PathVariable int roleid) {
        List<UserRole> userList;
        try {
            userList = userRoleService.findByRoleId(roleid);
            return R.ok("获取成功").put("data",userRoleService);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().put("data",null);
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param userId
     * @return
     */
    @ApiOperation("根据角色id批量查找")
    @GetMapping("/findBatchByUserId/{userId}")
    public R findBatchByUserId(@PathVariable Integer userId){

        try {
            return R.ok("获取成功").put("data",userRoleService.findBatchByUserId(userId));

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
}
